<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="${pageContext.request.contextPath }/layui/layui_2/layui/css/layui.css" media="all"/>
</head>
<body>
	<div id='update_dept_div'>
	  	<form class="layui-form" action="deptAction_update" id="updateDeptForm" method="post" lay-filter="update_emp_form">
	  		<input type="hidden" name="p_id" value="${dept.p_id }">
	  		<div class="layui-form-item" style="font-size: 18px;background: #5FB878;">
	  			<label class="layui-form-label">修改部门</label>
	  		</div>
	  		<div class="layui-form-item">
			    <label class="layui-form-label">部门名称</label>
			    <div class="layui-input-inline">
			      <input type="text" id="pName" name="p_name" value="${dept.p_name }"  placeholder="请输入部门名称" class="layui-input">
			    </div>
			</div>
	  		<div class="layui-form-item layui-form-text">
    			<label class="layui-form-label">部门备注</label>
	    		<div class="layui-input-block">
	      			<textarea name="p_remark" placeholder="请输入内容" class="layui-textarea">${dept.p_remark }</textarea>
	    		</div>
  			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
			    	<button class="layui-btn" lay-submit="" lay-filter="update" id="close">修改部门</button>
			    </div>
			</div>
	  	</form>
	</div>
	<script src="${pageContext.request.contextPath }/layui/layui_2/layui/layui.js"></script>
	<script>
	    layui.use(['element', 'table', 'jquery', 'layer', 'form', 'upload'], function () {
	        var $ = layui.jquery,
	            layer = layui.layer,
	            form = layui.form,
	            upload = layui.upload;
	       
	        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	        //监听提交
	        form.on('submit(update_emp_form)', function (data) {
	        	var name = "${dept.p_name }";
	            var value = document.getElementById("pName").value;
	            if(name == value){
	            	submit_form();
	            }else{
	            	$.post("${pageContext.request.contextPath}/deptAction_findByName.action",
		           	{p_name: value},
		            function (data) {
		            	if (data.result == 'notok') {
		                	// layer.tips 吸附弹出
		                    layer.tips('该部门已经存在', "#pName");
		                  	$("#pName").focus();
		                } else {
		                    submit_form();
		                }
		            });
	            }
				//阻止表单跳转。如果需要表单跳转，去掉这段即可。
				return false;
	        });
	        function submit_form() {
                $.post("${pageContext.request.contextPath}/deptAction_update.action",
                    $("#updateDeptForm").serialize(),
                    function (data) {
                        parent.location.href = '${pageContext.request.contextPath}/page_dept_dept.action';
                        parent.layer.close(index);
                });
           	}
	    });
	</script>
</body>
</html>